/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.examples;

import br.ufba.lasid.cloud.Cloud;
import br.ufba.lasid.cloud.Datacenter;
import br.ufba.lasid.cloud.Node;
import br.ufba.lasid.v1.utils.Constants;
import br.ufba.lasid.virt.HypervisorLibVirtXen;
import br.ufba.lasid.virt.IHypervisor;
import br.ufba.lasid.virt.VirtualMachine;

/**
 *
 * @author aliriosa
 */
public class CloudApp {

    public static void main(String[] args) throws Exception {

        /**
         * to execute run this on the terminal: cd
         * /home/csgrid/NetBeansProjects/jit-clouds-mda/target; java -cp
         * .:../tmp/jna.jar:../tmp/libvirt-0.4.9.jar:jit-clouds-mda-1.0-SNAPSHOT.jar
         * br.ufba.lasid.examples.CloudApp
         */
        Cloud cloud = new Cloud("lasid-cloud");

        Datacenter dc1 = new Datacenter("LaSiD-DC1");
        Datacenter dc2 = new Datacenter("LaSiD-DC2");

        cloud.addDatacenter(dc1);
        cloud.addDatacenter(dc2);

        Node n4 = new Node("192.168.188.4");
        Node n7 = new Node("192.168.188.7");

        dc1.addNode(n4);
        dc1.addNode(n7);

//        dc2.addNode(n3);
//        dc2.addNode(n4);
//        dc2.addNode(n5);

//        HypervisorLibVirtXen xen1 = new HypervisorLibVirtXen();
//        HypervisorLibVirtKVM kvm1 = new HypervisorLibVirtKVM();
//        HypervisorLibVirtXen xen2 = new HypervisorLibVirtXen();
//        HypervisorLibVirtXen xen3 = new HypervisorLibVirtXen();
        HypervisorLibVirtXen xen4 = new HypervisorLibVirtXen();
        HypervisorLibVirtXen xen7 = new HypervisorLibVirtXen();

//        n1.addHypervisor(xen1);
//        n2.addHypervisor(xen2);
//        n3.addHypervisor(xen3);
        n4.addHypervisor(xen4, true);
        n7.addHypervisor(xen7, true);

        /**
         * For each datacenter in my cloud.
         */
        System.out.println("\n------------ Listing Dataventers ------------");
        for (Datacenter dc : cloud.getDatacenters()) {
            System.out.println("Datacenter = " + dc.getId());
            /**
             * For each node in a datacenter.
             */
            if (dc.getNodes() != null) {
                System.out.println("---- Listing Nodes ----");
                for (Node node : dc.getNodes()) {
                    System.out.println("   Node(" + node.getAddress() + ") at Datacenter "
                            + dc1.getId());

                    /**
                     * For each hypervisor in a node.
                     */
                    IHypervisor hypervisor = node.getDefaultHypervisor();

                    hypervisor.connect();
                    System.out.println("   --" + hypervisor.getName() + " is installed at Node(" + node.getAddress() + ")");
                    hypervisor.configureVirtualMachines();
                    if (hypervisor.getVirtualMachines() != null) {
                        System.out.println("   ---- Listing Virtual Machines ----");
                        for (VirtualMachine vm : hypervisor.getVirtualMachines()) {
                            System.out.println("      -- " + vm.getDomainInfo());
                        }
                        System.out.println("      " + Constants.DASH);
                    }
                    hypervisor.disconnect();
                }
                System.out.println("   " + Constants.DASH + Constants.NEW_LINE);
            }
            System.out.println(Constants.DASH + Constants.NEW_LINE);
        }

        Datacenter dc = cloud.getDatacenter("LaSiD-DC1");

        Node node = dc.getNode("192.168.188.7");

        IHypervisor hypervisor = node.getDefaultHypervisor();

        /**
         * hypervisor.connect() uses the related node address to connect with
         * the disered hypervisor.
         */
        hypervisor.connect();
        hypervisor.configureVirtualMachines();

        VirtualMachine vm1 = hypervisor.getVirtualMachine("vm13");
        if (vm1 != null) {
            String vmXMLDescription = vm1.getDomain().getXMLDesc(0);
            System.out.println(vmXMLDescription);
            /**
             * hypervisor.newVMInstance(...) must check if other instance has
             * been initiated.
             */
            vm1.destroy();
            VirtualMachine vm2 = hypervisor.newVMInstance(vmXMLDescription);

            hypervisor.migrate(vm2.getDomain().getID(), "192.168.188.4");
        }
        hypervisor.disconnect();

        node = dc.getNode("192.168.188.4");
        hypervisor = node.getDefaultHypervisor();
        hypervisor.connect();
        hypervisor.configureVirtualMachines();
        vm1 = hypervisor.getVirtualMachine("vm13");
        hypervisor.migrate(vm1.getDomain().getID(), "192.168.188.7");

        hypervisor.disconnect();
        /**
         * Install failover
         */
//        IHypervisor h1 = n4.getDefaultHypervisor();
//        IHypervisor h2 = n7.getDefaultHypervisor();
//
//        VirtualMachine primaryVM = h1.newVMInstance("path_file_VM002.xml");
//
//        VirtualMachineFailover failover = new VirtualMachineFailover();
//
//        failover.install(primaryVM, h2);
    }
}
